package com.css.zfzx.fxpg.modules.buildings.census.repository.impl;

import com.css.bpm.platform.utils.PlatformObjectUtils;
import com.css.zfzx.fxpg.common.utils.cluster.Bounds;
import com.css.zfzx.fxpg.common.utils.cluster.Point;
import com.css.zfzx.fxpg.modules.buildings.census.repository.FxCountryFzHouseNativeRepository;
import com.css.zfzx.fxpg.modules.buildings.census.repository.entity.FxCountryFzHouseEntity;
import com.css.zfzx.fxpg.modules.buildings.census.viewobjects.FxHouseQueryParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author andy
 * @date 2020-11-11
 */
@Repository
public class FxCountryFzHouseNativeRepositoryImpl implements FxCountryFzHouseNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(FxCountryFzHouseNativeRepositoryImpl.class);

    @Override
    public Page<FxCountryFzHouseEntity> queryFxCountryFzHouses(FxHouseQueryParams queryParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from risk_census_country_fz_house ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province = :province");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            whereSql.append(" and city = :city");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            whereSql.append(" and area = :area");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getYear())) {
            whereSql.append(" and year = :year");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getBuildingName())) {
            whereSql.append(" and building_name like :buildingName");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getStructureType())) {
            whereSql.append(" and structure_type = :structureType");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxCountryFzHouseEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from risk_census_country_fz_house ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", queryParams.getProvince());
            countQuery.setParameter("province", queryParams.getProvince());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            query.setParameter("city", queryParams.getCity());
            countQuery.setParameter("city", queryParams.getCity());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            query.setParameter("area", queryParams.getArea());
            countQuery.setParameter("area", queryParams.getArea());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getYear())) {
            Date year = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                year = sf.parse(queryParams.getYear());
            } catch (Exception e) {
                log.error("日期格式转换错误");
            }
            query.setParameter("year", year);
            countQuery.setParameter("year", year);
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getBuildingName())) {
            query.setParameter("buildingName", "%" + queryParams.getBuildingName() + "%");
            countQuery.setParameter("buildingName", "%" + queryParams.getBuildingName() + "%");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getStructureType())) {
            query.setParameter("structureType", queryParams.getStructureType());
            countQuery.setParameter("structureType", queryParams.getStructureType());
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<FxCountryFzHouseEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<FxCountryFzHouseEntity> fxBuildingsPage = new PageImpl<>(list, pageable, count.longValue());
        return fxBuildingsPage;
    }

    @Override
    public List<FxCountryFzHouseEntity> listFxCountryFzHouses(FxHouseQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from risk_census_country_fz_house ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province like :province");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            whereSql.append(" and city like :city");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            whereSql.append(" and area like :area");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getYear())) {
            whereSql.append(" and year = :year");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getBuildingName())) {
            whereSql.append(" and building_name like :buildingName");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getStructureType())) {
            whereSql.append(" and structure_type = :structureType");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxCountryFzHouseEntity.class);
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", "%" + queryParams.getProvince() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            query.setParameter("city", "%" + queryParams.getCity() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            query.setParameter("area", "%" + queryParams.getArea() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getYear())) {
            query.setParameter("year", queryParams.getYear());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getBuildingName())) {
            query.setParameter("buildingName", queryParams.getBuildingName());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getStructureType())) {
            query.setParameter("structureType", queryParams.getStructureType());
        }
        List<FxCountryFzHouseEntity> list = query.getResultList();
        return list;
    }

    @Override
    public List<FxCountryFzHouseEntity> searchFxCountryFzHouses(FxHouseQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from risk_census_country_fz_house ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province like :province");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            whereSql.append(" and city like :city");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            whereSql.append(" and area like :area");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getYear())) {
            whereSql.append(" and year = :year");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getBuildingName())) {
            whereSql.append(" and building_name like :buildingName");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getStructureType())) {
            whereSql.append(" and structure_type = :structureType");
        }

        Bounds bounds = null;
        if (!PlatformObjectUtils.isEmpty(queryParams.getBounds())) {
            String coordinate = queryParams.getBounds();
            String[] coords = coordinate.split(",");
            String[] coord0 = coords[0].split(" ");
            String[] coord1 = coords[1].split(" ");
            bounds = new Bounds(new Point(Float.parseFloat(coord0[0]), Float.parseFloat(coord0[1])), new Point(Float.parseFloat(coord1[0]), Float.parseFloat(coord1[1])));
            //whereSql.append(" and MBRContains(ST_GeometryFromText('POLYGON ((" + coords[0] + "," + (coord0[0] + " " + coord1[1]) + "," + coords[1] + "," + (coord1[0] + " " + coord0[1]) + "," + coords[0] + "))'), geo_point)");
        }


        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxCountryFzHouseEntity.class);
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", "%" + queryParams.getProvince() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            query.setParameter("city", "%" + queryParams.getCity() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            query.setParameter("area", "%" + queryParams.getArea() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getYear())) {
            query.setParameter("year", queryParams.getYear());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getBuildingName())) {
            query.setParameter("buildingName", queryParams.getBuildingName());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getStructureType())) {
            query.setParameter("structureType", queryParams.getStructureType());
        }
        List<FxCountryFzHouseEntity> list = query.getResultList();
        return list;
    }

}
